package duc.cm.t2;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;

public class AnimalPangActivity extends Activity {
	private ImageView thu, bua;
	private Animal animal;
	private boolean activeThread = true;
	private GridView gridView;
	private ImageAdapter imageAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main);

		 thu = (ImageView) findViewById(R.id.thu1);
		 thu.setBackgroundResource(R.drawable.ocat);
		// setlayout(boy, 10, 230, 0, 0);

		// bua = (ImageView) findViewById(R.id.bua);
		// bua.setBackgroundResource(R.drawable.bua0);
		// setlayout(girl, 300, 230, 0, 0);
		// girl.setOnTouchListener(this);

		animal = new Animal(thu, 10, 230, 0, 0);
		// animal.setlayout(thu, animal.getX(), animal.getY(), 0, 0);
		// animal.animation(boy, R.drawable.boy);
		imageAdapter = new ImageAdapter(this);
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(imageAdapter);

		Thread myThread = new Thread() {
			@Override
			public void run() {
				try {
					int timeCounter = 500;
					while (activeThread && (timeCounter > 0)) {
						sleep(100);
						if (timeCounter % 10 == 0) {
							threadHandler
									.sendEmptyMessage((int) timeCounter / 10);
						}
						timeCounter--;
					}
				} catch (InterruptedException e) {
					
				}finally{
					finish();
				}
			}
		};

		// starting thread
		 myThread.start();
	}

//	public boolean onTouchEvent(MotionEvent event) {
//		if (event.getAction() == MotionEvent.ACTION_DOWN) {
//			animal.setlayout(bua, (int) event.getX(), (int) event.getY(), -100,
//					-100);
//			animal.animation(bua, R.drawable.bua);
//		}
//		return false;
//	}

	private Handler threadHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if ((msg.what) % 3 == 0) {
//				Random rnd = new Random();
//				animal.setlayout(thu, rnd.nextInt(5) * 80, rnd.nextInt(8) * 80,
//						-100, -100);
//				animal.animation(thu, R.drawable.thu);
//				up();
				for(int i=0;i<gridView.getChildCount();i++){
					Log.d("pos", Integer.toString(i));
				}
				animal.animation(thu, R.drawable.thu);
			}
		}
	};
	
//	public void up(){
//		animal.animation(gridView.getChildAt(gridView.getFirstVisiblePosition()), R.drawable.thu);
//	}

}